home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_uu.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.8 KB  |  159 lines

  1. """
  2. Tests for uu module.
  3. Nick Mathewson
  4. """
  5.  
  6. from test_support import verify, TestFailed, verbose, TESTFN
  7. import sys, os
  8. import uu
  9. from StringIO import StringIO
  10.  
  11. teststr = "The smooth-scaled python crept over the sleeping dog\n"
  12. expected = """\
  13. M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
  14. (:6YG(&1O9PH """
  15. encoded1 = "begin 666 t1\n"+expected+"\n \nend\n"
  16. if verbose:
  17.     print '1. encode file->file'
  18. inp = StringIO(teststr)
  19. out = StringIO()
  20. uu.encode(inp, out, "t1")
  21. verify(out.getvalue() == encoded1)
  22. inp = StringIO(teststr)
  23. out = StringIO()
  24. uu.encode(inp, out, "t1", 0644)
  25. verify(out.getvalue() == "begin 644 t1\n"+expected+"\n \nend\n")
  26.  
  27. if verbose:
  28.     print '2. decode file->file'
  29. inp = StringIO(encoded1)
  30. out = StringIO()
  31. uu.decode(inp, out)
  32. verify(out.getvalue() == teststr)
  33. inp = StringIO("""UUencoded files may contain many lines,
  34.                   even some that have 'begin' in them.\n"""+encoded1)
  35. out = StringIO()
  36. uu.decode(inp, out)
  37. verify(out.getvalue() == teststr)
  38.  
  39. stdinsave = sys.stdin
  40. stdoutsave = sys.stdout
  41. try:
  42.     if verbose:
  43.         print '3. encode stdin->stdout'
  44.     sys.stdin = StringIO(teststr)
  45.     sys.stdout = StringIO()
  46.     uu.encode("-", "-", "t1", 0666)
  47.     verify(sys.stdout.getvalue() == encoded1)
  48.     if verbose:
  49.         print >>stdoutsave, '4. decode stdin->stdout'
  50.     sys.stdin = StringIO(encoded1)
  51.     sys.stdout = StringIO()
  52.     uu.decode("-", "-")
  53.     verify(sys.stdout.getvalue() == teststr)
  54. finally:
  55.     sys.stdin = stdinsave
  56.     sys.stdout = stdoutsave
  57.  
  58. if verbose:
  59.     print '5. encode file->file'
  60. tmpIn  = TESTFN + "i"
  61. tmpOut = TESTFN + "o"
  62. try:
  63.     fin = open(tmpIn, 'wb')
  64.     fin.write(teststr)
  65.     fin.close()
  66.  
  67.     fin = open(tmpIn, 'rb')
  68.     fout = open(tmpOut, 'w')
  69.     uu.encode(fin, fout, tmpIn, mode=0644)
  70.     fin.close()
  71.     fout.close()
  72.  
  73.     fout = open(tmpOut, 'r')
  74.     s = fout.read()
  75.     fout.close()
  76.     verify(s == 'begin 644 ' + tmpIn + '\n' + expected + '\n \nend\n')
  77.  
  78.     os.unlink(tmpIn)
  79.     if verbose:
  80.         print '6. decode file-> file'
  81.     uu.decode(tmpOut)
  82.     fin = open(tmpIn, 'rb')
  83.     s = fin.read()
  84.     fin.close()
  85.     verify(s == teststr)
  86.     # XXX is there an xp way to verify the mode?
  87.  
  88. finally:
  89.     try:
  90.         fin.close()
  91.     except:
  92.         pass
  93.     try:
  94.         fout.close()
  95.     except:
  96.         pass
  97.     try:
  98.         os.unlink(tmpIn)
  99.     except:
  100.         pass
  101.     try:
  102.         os.unlink(tmpOut)
  103.     except:
  104.         pass
  105.  
  106. if verbose:
  107.     print '7. error: truncated input'
  108. inp = StringIO("begin 644 t1\n"+expected)
  109. out = StringIO()
  110. try:
  111.     uu.decode(inp, out)
  112.     raise TestFailed("No exception thrown")
  113. except uu.Error, e:
  114.     verify(str(e) == 'Truncated input file')
  115.  
  116. if verbose:
  117.     print '8. error: missing begin'
  118. inp = StringIO("")
  119. out = StringIO()
  120. try:
  121.     uu.decode(inp, out)
  122.     raise TestFailed("No exception thrown")
  123. except uu.Error, e:
  124.     verify(str(e) == 'No valid begin line found in input file')
  125.  
  126. # Test to verify that decode() will refuse to overwrite an existing file
  127. import tempfile
  128. outfile = tempfile.mktemp()
  129. inp = StringIO('Here is a message to be uuencoded')
  130. out = StringIO()
  131. uu.encode(inp, out, outfile)
  132. out.seek(0)
  133. try:
  134.     if verbose:
  135.         print '9. decode w/file not exists is okay'
  136.     uu.decode(out)
  137.     if not os.path.exists(outfile):
  138.         raise TestFailed('uudecode w/ out_file=None failed')
  139.     fp = open(outfile)
  140.     data = fp.read()
  141.     fp.close()
  142.     if data <> inp.getvalue():
  143.         raise TestFailed('uudecode stored something weird')
  144.     # Try to write it again, which should cause a failure
  145.     if verbose:
  146.         print '10. uudecode w/file exists fails'
  147.     out.seek(0)
  148.     try:
  149.         uu.decode(out)
  150.     except uu.Error:
  151.         pass
  152.     else:
  153.         raise TestFailed('expected to get a "file exists" error')
  154. finally:
  155.     try:
  156.         os.unlink(outfile)
  157.     except OSError:
  158.         pass
  159.